/*
 * TelephoneSelectionView.java
 *
 * Created on August 25, 2006, 7:56 PM
 */
package com.floreantpos.ui.views;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import com.floreantpos.IconFactory;
import com.floreantpos.swing.MessageDialog;
import com.floreantpos.swing.PosButton;
import com.floreantpos.swing.TransparentPanel;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author MShahriar
 */
public class TelephoneSelectionView extends TransparentPanel implements ActionListener {

  private TitledBorder titledBorder;
  private javax.swing.JFormattedTextField tfNumber;
  private String userEntry = "";
  private String formattedNumber;
  private int userEntryLenght = 0;

  /**
   * Creates new form NumberSelectionView
   */
  public TelephoneSelectionView() {
    initComponents();
  }

  private void initComponents() {
    setLayout(new BorderLayout(5, 5));

    tfNumber = new JFormattedTextField(createFormatter("(##) *####-####"));
    tfNumber.setText("(00) 00000-0000");
    tfNumber.setFont(tfNumber.getFont().deriveFont(Font.BOLD, 24));
    tfNumber.setEditable(false);
    tfNumber.setBackground(Color.WHITE);
    tfNumber.setHorizontalAlignment(JTextField.RIGHT);

    JPanel northPanel = new JPanel(new BorderLayout(5, 5));
    northPanel.add(tfNumber, BorderLayout.CENTER);

    PosButton btnClearAll = new PosButton();
    btnClearAll.setText(com.floreantpos.POSConstants.CLEAR_ALL);
    btnClearAll.setActionCommand(com.floreantpos.POSConstants.CLEAR_ALL);
    btnClearAll.setPreferredSize(new Dimension(90, 50));
    btnClearAll.addActionListener(this);
    northPanel.add(btnClearAll, BorderLayout.EAST);

    add(northPanel, BorderLayout.NORTH);

    String[][] numbers = {
      {"7", "8", "9"},
      {"4", "5", "6"},
      {"1", "2", "3"},
      {" ", "0", " "}
    };
    String[][] iconNames = new String[][]{
      {"7_32.png", "8_32.png", "9_32.png"},
      {"4_32.png", "5_32.png", "6_32.png"},
      {"1_32.png", "2_32.png", "3_32.png"},
      {"empty_32.png", "0_32.png", "empty_32.png"}
    };

    JPanel centerPanel = new JPanel(new GridLayout(4, 3, 5, 5));
    Dimension preferredSize = new Dimension(90, 80);

    for (int i = 0; i < numbers.length; i++) {
      for (int j = 0; j < numbers[i].length; j++) {
        PosButton posButton = new PosButton();
        ImageIcon icon = IconFactory.getIcon(iconNames[i][j]);
        String buttonText = String.valueOf(numbers[i][j]);

        if (icon == null) {
          posButton.setText(buttonText);
        } else {
          posButton.setIcon(icon);
        }

        posButton.setActionCommand(buttonText);
        posButton.setPreferredSize(preferredSize);
        posButton.addActionListener(this);
        centerPanel.add(posButton);
      }
    }
    add(centerPanel, BorderLayout.CENTER);

    titledBorder = new TitledBorder("");
    titledBorder.setTitleJustification(TitledBorder.CENTER);

    setBorder(titledBorder);
  }

  public void actionPerformed(ActionEvent e) {
    String actionCommand = e.getActionCommand();
    if (actionCommand.equals(com.floreantpos.POSConstants.CLEAR_ALL)) {
      userEntryLenght = 0;
      userEntry = "";
      formattedNumber = "";
      tfNumber.setText("(00) 00000-0000");
    } else {
      userEntry = userEntry + actionCommand;
      userEntryLenght = userEntry.length();

      if (userEntryLenght == 1) {
        formattedNumber = "(" + userEntry + "0) 00000-0000";
      } else if (userEntryLenght == 2) {
        formattedNumber = "(" + userEntry + ") 00000-0000";
      } else if (userEntryLenght == 3) {
        formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.charAt(2) + "0000-0000";
      } else if (userEntryLenght == 4) {
        formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 4) + "000-0000";
      } else if (userEntryLenght == 5) {
        formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 5) + "00-0000";
      } else if (userEntryLenght == 6) {
        formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 6) + "0-0000";
      } else if (userEntryLenght == 7) {
        formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 7) + "-0000";
      } else if (userEntryLenght == 8) {
        formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 7) + "-" + userEntry.charAt(7) + "000";
      } else if (userEntryLenght == 9) {
        formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 7) + "-" + userEntry.substring(7, 9) + "00";
      } else if (userEntryLenght == 10) {
        formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 7) + "-" + userEntry.substring(7, 10) + "0";
      } else if (userEntryLenght == 11) {
        formattedNumber = "(" + userEntry.substring(0, 2) + ") " + userEntry.substring(2, 7) + "-" + userEntry.substring(7, 11);
      }

      tfNumber.setText(formattedNumber);
    }
  }

  public void setTitle(String title) {
    titledBorder.setTitle(title);
  }

  public String getValue() {
    return tfNumber.getText();
  }

  public void setValue(String value) {
    tfNumber.setText(value);
  }

  protected MaskFormatter createFormatter(String s) {
    MaskFormatter formatter = null;
    try {
      formatter = new MaskFormatter(s);
    } catch (Exception exc) {
      MessageDialog.showError(com.floreantpos.POSConstants.INVALID_PHONE_NUMBER + exc.getMessage());
    }
    return formatter;
  }

}
